package com.app.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.app.bean.Kcxx;
import com.app.bean.KcxxExample;
import com.app.bean.KcxxExample.Criteria;
import com.app.bean.Slxx;
import com.app.bean.SlxxExample;
import com.app.mapper.KcxxMapper;
import com.app.mapper.SlxxMapper;
import com.pro.util.StringUtil;

import net.sf.json.JSONArray;

@Controller
@RequestMapping("/slxx")
public class SlxxController  extends BaseController{

	@Autowired
	private SlxxMapper slxxMapper;
	
	@Autowired
	private KcxxMapper kcxxMapper;
	
	@RequestMapping("/check")
	public void check(HttpServletRequest request,HttpServletResponse response,Slxx slxx){
		init(request);
		
		try{
			
			//回退必须填写回退理由
			if(slxx.getStatus().equals("2")){
				
				if(StringUtil.isEmpty(slxx.getRefuseCause())){
					result.setCode(-1);
					result.setErrMsg("请填写回退理由！");
					endProcess(response, result);
					return;
				}

			}
			
			slxxMapper.updateByPrimaryKeySelective(slxx);
			
			//审核通过还需要更改库存数量
			if(slxx.getStatus().equals("1")){
				
				Kcxx kcxx = kcxxMapper.selectByPrimaryKey(slxx.getWpid());
				
				kcxx.setWpCount(kcxx.getWpCount() - slxx.getWpCount()); //更新库存数量
				
				kcxxMapper.updateByPrimaryKeySelective(kcxx);

				
			}
			
			
		}catch(Exception e){
			e.printStackTrace();
			result.setCode(-2);
			result.setErrMsg("系统错误" + e.getMessage());
		}
		
		
		endProcess(response, result);
		
	}
	
	@RequestMapping("/list")
	public void list(HttpServletRequest request,HttpServletResponse response,String status){
		init(request);
		
		try{
			SlxxExample example = new SlxxExample();
			com.app.bean.SlxxExample.Criteria criteria = example.createCriteria();

			if(status == null){
				criteria.andStatusEqualTo("0"); //只查询待审核
			}else{
				criteria.andStatusEqualTo(status);
			}
			
			List<Slxx> list = slxxMapper.selectByExample(example);
			result.setRows(JSONArray.fromObject(list));
			
		}catch(Exception e){
			e.printStackTrace();
			result.setCode(-2);
			result.setErrMsg("系统错误" + e.getMessage());
		}
		
		
		endProcess(response, result);
		
	}
	
}
